package com.yoomiito.app.ui.anyview.goodsinfo.header

import android.content.Context
import android.text.Spannable
import android.text.SpannableString
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.LinearLayout
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.bean.GoodsDetail
import com.yoomiito.app.utils.*
import kotlinx.android.synthetic.main.item_coupon_view.view.*
import kotlinx.android.synthetic.main.item_goods_detail_price_1.view.*
import kotlinx.android.synthetic.main.item_goods_detail_price_2.view.*
import kotlinx.android.synthetic.main.video_play_view.view.*
import kotlinx.android.synthetic.main.view_goodsinfo_header.view.*
import kotlinx.android.synthetic.main.item_goods_detail_price_1.view.oldPrice as oldPrice1
import kotlinx.android.synthetic.main.item_goods_detail_price_2.view.price as price1

class LiBaoGoodsInfoHeaderView(context: BaseActivity<*>) : BaseGoodsInfoHeaderView(context) {
    override fun getLayoutId(): Int = R.layout.view_goodsinfo_header

    override fun getParentView(): LinearLayout = this

    override fun initView(goodsDetail: GoodsDetail) {
        initBanner(banner, goodsDetail)

        //不登录 和 Vip及以上级别 样式一样
        val isVip = !App.mAppConfig.isLogin || UserInfoConfig.getRoleId() >= Type.LevelType.VIP_10
        getVideoTime(goodsDetail) {
            playVideoView.visibility = View.VISIBLE
            durationTv.text = DateUtils.ms2min(it.duration)
            playVideoView.setOnClickListener { _ -> OnVideoClickListener(it) }

            //去视频
        }
        goodsDesTv.visibility = View.GONE
        if (viewStubIsInflate(R.id.priceLlVs)) {
            priceLlVs.layoutResource = if (isVip) R.layout.item_goods_detail_price_2 else R.layout.item_goods_detail_price_1
            priceLlVs.inflate()
        }
        val priceTv = if (isVip) price1 else {
            oldPrice1.visibility = View.GONE
            price
        }
        goodsTitleTv.setOnClickListener {
              if (App.mAppConfig.isLogin){
                showShareDialog()
            }else{
//                AllUtils.toLogin(context, AllCode.LoginIn)
                LoginUtils.toOneKeyLogin(context)
            }
        }
//        val awardViewTv = if (isVip) awardView else null
//        val couponViewTv = if (isVip) couponView else couponView1
        //送优米 赚
        //赚显示规则：未登录不显示；会员不显示  粉丝及普通VIP显示后台配置的基础佣金；VIP及VIP以上显示【7*佣金倍数】
//        awardViewTv?.visibility = if (!App.mAppConfig.isLogin || UserInfoConfig.getRoleId() < Type.LevelType.VIP_4 || SpannableUtils.isEmptyOr0(goodsDetail.get_money)) View.GONE else View.VISIBLE
//        awardViewTv?.setText("¥${goodsDetail.salePrice}")
//        awardViewTv?.setTextSize(12f)

        //优米 有就显示
//        couponViewTv.setText("${goodsDetail.yo_money}优米")
//        couponViewTv.setHeaderText("送")
//        couponViewTv.setTextSize(12f)
//        couponViewTv.visibility = if (goodsDetail.yo_money == 0) View.GONE else View.VISIBLE
//        val priceT = goodsDetail.price.removeDecimalPoint()
//        val oldPriceT = goodsDetail.reserve_price.removeDecimalPoint()
//        val priceStr = if (oldPriceT.isEmptyOr0()) "¥${priceT} " else "¥${priceT} ¥${oldPriceT}"
//        if (!isVip) {
//            bg_1.setImageResource(R.drawable.bg_libao_1)
//            bg_2.setImageResource(R.drawable.bg_libao_2)
//
//            couponView.setColor(ResUtil.getColor(R.color.color_FFEAB4), ResUtil.getColor(R.color.color_1AFFEAB4))
//            //价格
//            priceTv.setTextColor(ContextCompat.getColor(context, R.color.colorAccent))
//
//        }
//        priceTv.text = priceStr.goodsPrice(priceT,true,R.color.color_balk_999999,0.375f)
        priceTv.text = "¥" + goodsDetail.salePrice
        priceTv.visibility = View.VISIBLE
        //标题
        goodsTitleTv.text = goodsDetail.title
        goodsTitleCopy(goodsTitleTv, goodsDetail.title)

        //销量 及分享件数
        val size = "已抢${goodsDetail.volume}件"
        val sizeSpannableString = SpannableString(size)
        sizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, size.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//销量颜色
        goodsSaleSizeTv.text = sizeSpannableString

        val shareSize = "分享${goodsDetail.share_num}次"
        val shareSizeSpannableString = SpannableString(shareSize)
        shareSizeSpannableString.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_333333)), 2, shareSize.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)//分享件数颜色
        goodsShareSizeTv.text = shareSizeSpannableString

        if (viewStubIsInflate(R.id.itemGoodsVs)) {
            itemGoodsVs.layoutResource = R.layout.item_goods_flag_1
            itemGoodsVs.inflate()
        }
    }

}